//实现一个单向、不带头、非循环的链表
public class MySingleList implements ISingleList{

    static class ListNode{
        private int val;
        private ListNode next;
        //首先创建一个构造方法初始化链表
        public ListNode(int val) {
            this.val = val;
            this.next = null;
        }
    }

    //链表的头节点
    private ListNode head;
    private int listSize;
    /**
     * 实现链表的头插
     * @param data
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }
        listSize++;
    }

    /**
     * 实现链表的尾插
     * @param data
     */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //如果链表为空，则可以直接头插
        if(head == null) {
            head = node;
            listSize++;
            return;
        }
        ListNode cur = head;
        //遍历链表,找到最后一个结点
        //如果让cur=null，会使得这个新的节点插入后与原来的链表脱节
        //需要记录最后一个节点
//        while(cur != null) {
//            cur = cur.next;
//        }
//        cur = node;


        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
        listSize++;
    }

    //查找下标位置的节点
    private ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while(count < index - 1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    @Override
    public void addIndex(int index, int data) throws ListIndexOutOfBoundsException {
        //首先检查下标是否越界
        checkIndex(index);
        //然后处理特殊情况,第一个节点和最后一个节点
        if(head == null) {
            addFirst(data);
            return;
        }
        if(index == getSize()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
        listSize++;
    }

    private void checkIndex(int index) throws ListIndexOutOfBoundsException{
        if(index < 0 || index > getSize()){
            throw new IndexOutOfBoundsException("该下标不是正确的下标内容，index = " + index);
        }
    }
    @Override
    public boolean contains(int key) {
        if(head == null){
            return false;
        }
        ListNode cur = head;
        //遍历链表
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            System.out.println("没有要删除的数据");
            return;
        }
        //处理特殊情况，首元素
        if(head.val == key){
            head = head.next;
            listSize--;
            return;
        }

        ListNode cur = head;
        //遍历链表，将对应的值删除
        while(cur.next != null){
            if(cur.next.val == key){
                cur.next = cur.next.next;
                listSize--;
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有找到你要删除的数据");
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                listSize--;
            }else {
               prev = cur;
            }
            cur = cur.next;
        }
        if(head.val == key){
            head = head.next;
            listSize--;
        }
    }

    @Override
    public int getSize() {
        return listSize;
    }

    @Override
    public void clear() {
        head = null;
        listSize = 0;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode reverseList(){
        //特殊情况
        if(head == null){
            return null;
        }
        //链表中只有一个元素
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;//第一步
            cur.next = head;
            head = cur;
            cur = curNext;
            //这里如果将curNext向后移动放后面，
            //再将第一步挪到循环外面会使得在反转最后一个元素时造成对空指针的引用
//            curNext = cur.next;
        }
        System.out.println("反转成功！！！");
        return head;
    }
}
